package com.imgo.max.experience.service;

import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.log.CustomLogger;
import com.imgo.max.common.log.CustomLoggerFactory;
import com.imgo.max.common.util.page.PageVO;
import com.imgo.max.common.util.page.Pagination;
import com.imgo.max.experience.dto.DreamFundConsumeDto;
import com.imgo.max.experience.dto.PracticerExperienceDto;
import com.imgo.max.experience.dto.PracticerExperienceMappingDto;
import com.imgo.max.experience.dto.UserExperienceDto;
import com.imgo.max.experience.mapping.PracticerExperienceMapping;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 芒果练习生经验服务接口
 *
 * @author perter.Y
 */
@Component
public class PracticerExperienceServiceImpl implements PracticerExperienceService {
    private static final CustomLogger logger = CustomLoggerFactory.getLogger(PracticerExperienceServiceImpl.class);

    @Resource
    PracticerExperienceHandler practicerExperienceHandler;

    /**
     * 梦想基金消费接口
     *
     * @param uid
     * @param payTarget
     * @param targetId
     * @param amount
     * @return
     */
    public ResultDO<?> consumeDreamFund(DreamFundConsumeDto consumeDto) {
        ResultDO<?> practicerDo = practicerExperienceHandler.consumeDreamFund(consumeDto);
        return practicerDo;
    }


    /**
     * 查询梦想基金消费记录
     *
     * @param uid
     * @return
     */
    @Override
    public ResultDO<List<DreamFundConsumeDto>> queryDreamFundConsume(String uid, Pagination pagination) {
        ResultDO<List<DreamFundConsumeDto>> consumeLogs = practicerExperienceHandler.queryDreamFundConsume(uid, pagination);
        return consumeLogs;
    }


    /**
     * 获取用户经验
     *
     * @param uid 用户ID
     * @return
     */
    @Override
    public ResultDO<PracticerExperienceDto> getPracticerExperience(String uid) {
        PracticerExperienceDto practicerDto = (PracticerExperienceDto) practicerExperienceHandler.getUserExperience(uid).getModule();
        return new ResultDO<>(practicerDto);
    }

    @Override
    public ResultDO<Integer> getPracticerLevel(String uid) {
        ResultDO<Integer> result = practicerExperienceHandler.getUserLevel(uid);
        return result;
    }

    /**
     * 获取用户经验
     *
     * @param uid 用户ID
     * @return
     */
    @Override
    public ResultDO<List<PracticerExperienceDto>> getPracticersExperience(List<String> uids) {
        List<PracticerExperienceDto> praticers = new ArrayList();
        ResultDO<List<UserExperienceDto>> dtos = practicerExperienceHandler.getUsersExperience(uids);
        if (dtos.isSuccess() && !CollectionUtils.isEmpty(dtos.getModule())) {
            for (UserExperienceDto dto : dtos.getModule()) {
                praticers.add((PracticerExperienceDto) dto);
            }
        }
        return new ResultDO(praticers);
    }

    @Override
    public ResultDO<PageVO<PracticerExperienceDto>> getUsersExperienceByLevel(Integer beginLevel, Integer endLevel, Pagination pagination) {
        ResultDO<PageVO<PracticerExperienceDto>> dtos = practicerExperienceHandler.getUsersExperienceByLevel(beginLevel, endLevel, pagination);
        return dtos;
    }


    @Override
    public ResultDO<PageVO<PracticerExperienceDto>> getUsersExperienceByDreamFund(Integer beginFund, Integer endFund, Pagination pagination) {
        ResultDO<PageVO<PracticerExperienceDto>> dtos = practicerExperienceHandler.getUsersExperienceByDreamFund(beginFund, endFund, pagination);
        return dtos;
    }


    @Override
    public ResultDO<List<PracticerExperienceMappingDto>> getExprMappingList() {
        List<PracticerExperienceMapping> mappings = practicerExperienceHandler.getExprMappingList();
        if (CollectionUtils.isEmpty(mappings)) {
            return new ResultDO<>();
        }
        List<PracticerExperienceMappingDto> dtos = new ArrayList<>();
        for (PracticerExperienceMapping mapping : mappings) {
            PracticerExperienceMappingDto dto = new PracticerExperienceMappingDto();
            dto.setId(mapping.getId());
            dto.setDesc(mapping.getDesc());
            dto.setStartScore(mapping.getStartScore());
            dto.setEndScore(mapping.getEndScore());
            dto.setLevel(mapping.getLevel());
            dto.setLevelImage(mapping.getLevelImage());
            dtos.add(dto);
        }
        return new ResultDO<>(dtos);
    }

}
